package ui.accountbook;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JPanel;

import tool.MyButton;
import vo.AccountBookVo;
import vo.UserVo;
import blservice.accountbook.AccountBookBLService;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import client.boot.BLServiceManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AccountBookManagePanel extends JPanel{
	private static final long serialVersionUID = -2236738907036908347L;
	private UserVo user;
	private AccountBookBLService bl;
	private JTable table;
	private JScrollPane jsp;
	private List<AccountBookVo> list;
	public AccountBookManagePanel(UserVo user) {
		try {
			bl=BLServiceManager.getAccountBookBLService();
		} catch (RemoteException | MalformedURLException | NotBoundException
				| SQLException e) {
			JOptionPane.showMessageDialog(this,e.getMessage());
			e.printStackTrace();
		}
		this.user=user;
		this.setOpaque(false);
		this.setLayout(null);
		this.setBounds(160,40,646,433);
		this.setVisible(true);
		
		JButton button = new MyButton("Graphics/Button93-23.png");
		button.setText("\u65B0\u5EFA\u8D26\u76EE");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new AccountBookInfoDialog(true, AccountBookManagePanel.this.user, AccountBookManagePanel.this,new AccountBookVo()).setVisible(true);
			}
		});
		button.setBounds(108, 361, 93, 23);
		add(button);
		
		JButton button_1 = new MyButton("Graphics/Button140-23.png");
		button_1.setText("\u67E5\u8BE2\u671F\u521D\u4FE1\u606F");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row=table.getSelectedRow();
				if(row==-1){
					return;
				}
				new AccountBookInfoDialog(false, user, AccountBookManagePanel.this,list.get(row)).setVisible(true);
			}
		});
		button_1.setBounds(408, 361, 140, 23);
		add(button_1);
		
		table = new JTable(){
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		jsp = new JScrollPane(table);
		table.setBounds(108, 33, 440, 292);
		jsp.setBounds(108,33,440,292);
		add(jsp);
		
		showAll();
	}
	
	void showAll(){
		try {
			list=bl.getAll();
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this,"无法连接到服务器\n"+e.getMessage());
			e.printStackTrace();
			return;
		}
		table.setModel(new DefaultTableModel(readABVo(list), new String[]{"账目名称"}));
	}
	
	private String[][] readABVo(List<AccountBookVo> vo){
		String[][] result=new String[vo.size()][1];
		for(int i=0;i<vo.size();i++){
			result[i][0]=vo.get(i).getName();
			try {
				if(result[i][0].equals(bl.getDefaultAccountBook().getName())){
					result[i][0]=result[i][0]+"(当前账目)";
				}
			} catch (RemoteException e) {
				JOptionPane.showMessageDialog(this,"无法连接到服务器\n"+e.getMessage());
				e.printStackTrace();
			};
		}
		return result;
	}
}
